<template>
	<view class="wrap">
		<u-form :model="model" :rules="rules" ref="uForm">
			<u-form-item label-width="350"
				:label-position="labelPosition" label="缸套数量(个)" prop="cylinderCount">
				<u-input :border="border" placeholder="请输入缸套数量" v-model="model.cylinderCount" type="digit"></u-input>
			</u-form-item>
			<u-form-item label-width="350"
				:label-position="labelPosition" label="缸套内径(mm)" prop="cylinderDiameter">
				<u-input :border="border" placeholder="请输入缸套内径" v-model="model.cylinderDiameter" type="digit"></u-input>
			</u-form-item>
			<u-form-item label-width="350"
				:label-position="labelPosition" label="冲程(m)" prop="stroke">
				<u-input :border="border" placeholder="请输入冲程" v-model="model.stroke" type="digit"></u-input>
			</u-form-item>
		</u-form>

		<u-button type="primary" @click="submit">计算</u-button>

		<view class="u-config-wrap">
			<view class="u-config-item">
				<view class="u-item-title">计算结果</view>
				<u-form-item style="font-size: 22px;" :leftIconStyle="{color: '#888', fontSize: '32rpx'}" label-width="300"
					:label-position="labelPosition" label="柱塞泵1冲次的排量" prop="discharge">
					<span v-if="model.discharge"> {{model.discharge}} m³ </span>
				</u-form-item>
			</view>
			<view class="u-config-item">
				<view class="u-item-title">计算公式</view>
				<view class="u-item-desc">Q = nπD²L×10⁻⁶/4</view>
				<view class="u-item-desc">式中：</view>
				<view class="u-item-desc">Q — 柱塞泵1冲次的排量，m³</view>
				<view class="u-item-desc">n — 缸套数量，个</view>
				<view class="u-item-desc">D — 缸套内径，mm</view>
				<view class="u-item-desc">L — 冲程，m</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				model: {
					cylinderCount: '',
					cylinderDiameter: '',
					stroke: '',
					discharge: ''
				},
				rules: {
					cylinderCount: [{
						required: true,
						message: '请输入缸套数量',
						trigger: 'blur'
					}],
					cylinderDiameter: [{
						required: true,
						message: '请输入缸套内径',
						trigger: 'blur'
					}],
					stroke: [{
						required: true,
						message: '请输入冲程',
						trigger: 'blur'
					}]
				},
				labelPosition: 'left',
				border: true,
				
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						const n = parseFloat(this.model.cylinderCount);
						const D = parseFloat(this.model.cylinderDiameter);
						const L = parseFloat(this.model.stroke);
						
						// 计算柱塞泵1冲次的排量 Q = nπD²L×10⁻⁶/4
						this.model.discharge = ((n * Math.PI * Math.pow(D, 2) * L * Math.pow(10, -6)) / 4).toFixed(2);
					}
				});
			}
		}
	};
</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
	}

	.u-config-wrap {
		margin-top: 30rpx;
	}

	.u-config-title {
		padding: 20rpx;
		font-size: 32rpx;
		font-weight: bold;
	}

	.u-config-item {
		padding: 20rpx;
	}

	.u-item-title {
		font-size: 28rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
	}

	.u-item-desc {
		font-size: 24rpx;
		color: #666;
	}
</style> 